"""
Case Type   : 客户场景-邮储
Case Name   : DBCOMPATIBILITY='B'，创建int型非空列的表，插入null值
Create At   : 2021/11/25
Owner       : wan005
Description :
    1.创建数据库兼容B
    2.建表 int 非空
    3.插入null值
    4.清理环境
Expect      :
    1.创建成功
    2.成功
    3.成功
    4.成功
History     :
"""

import unittest
import os

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Finance(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.commonsh = CommonSH("PrimaryDbUser")
        self.user_node = Node("PrimaryDbUser")
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)
        status = self.commonsh.restart_db_cluster()
        self.log.info(status)
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertTrue("Normal" in status or "Degraded" in status)

        self.db_name = "db_finance_001"
        self.t_name = "t_finance_001"

    def test_finance(self):
        text = "--step1:创建兼容B的数据库; expect:成功"
        self.log.info(text)
        sql = f"drop database if exists {self.db_name};" \
            f" create database {self.db_name} DBCOMPATIBILITY 'B';"
        result = self.commonsh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      result, "执行失败:" + text)

        text = "--step2:建表 int 非空; expect:成功"
        self.log.info(text)
        create_sql = f"create table {self.t_name}(c_int int);"
        result = self.commonsh.execut_db_sql(create_sql,
                                             dbname=f"{self.db_name}")
        self.log.info(result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      result, "执行失败:" + text)

        text = "--step3:插入null值; expect:成功"
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(null);"
        result = self.commonsh.execut_db_sql(insert_sql,
                                             dbname=f"{self.db_name}")
        self.log.info(result)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      result, "执行失败:" + text)

        select_sql = f"select * from {self.t_name} where c_int is null;"
        result = self.commonsh.execut_db_sql(select_sql,
                                             dbname=f"{self.db_name}")
        self.log.info(result)
        self.assertIn("1 row", result, "执行失败:" + text)

        select_sql = f"select * from {self.t_name} where c_int='';"
        result = self.commonsh.execut_db_sql(select_sql,
                                             dbname=f"{self.db_name}")
        self.log.info(result)
        self.assertIn("0 row", result, "执行失败:" + text)

    def tearDown(self):
        text = "--step4:清理环境; expect:成功"
        self.log.info(text)
        sql = f"set dolphin.b_compatibility_mode = off;" \
            f"drop database {self.db_name};"
        result = self.commonsh.execut_db_sql(sql)
        self.log.info(result)
        status = self.commonsh.get_db_cluster_status("detail")
        self.log.info(status)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result,
                      "执行失败:" + text)
        self.assertTrue("Normal" in status or "Degraded" in status)
        self.log.info(f"----{os.path.basename(__file__)} finish----")
